const express = require('express');
const { query } = require('../mysql/query');
const router = express.Router();

/* GET home page. */
router.get('/', function (req, res, next) {
  res.send('room');
});


router.get('/getRoom', async function (req, res, next) {
  try {
    let result = await query(`SELECT * FROM ljz_meeting_room`);
    res.send(result);
  } catch (error) {
    res.send(error);
    throw (error);
  }
})

//增加会议室
//{"name":"101","num":"50"}
router.post('/addRoom', async function (req, res, next) {
  try {
    let { name, num } = req.body;
    await query(`INSERT INTO ljz_meeting_room (name,num) VALUES ('${name}',${num})`);
    let room_id = await query(`SELECT * FROM ljz_meeting_room WHERE name='${name}' and num='${num}'`);
    res.send(room_id);
  } catch (error) {
    res.status(300).send(error);
    throw (error);
  }
})

//移除会议室
//{"roomId":"1"}
router.post('/removeRoom', async function (req, res, next) {
  try {
    let { roomId } = req.body;
    await query(`DELETE FROM ljz_meeting_room WHERE room_id = ${roomId}`);
    res.send('success');
  } catch (error) {
    res.send(error);
    throw (error);
  }
})
//修改会议室
//{"roomId":"1","name":"101","num":"50"}
router.post('/editRoom', async function (req, res, next) {
  try {
    let { roomId, name, num } = req.body;
    await query(`UPDATE ljz_meeting_room SET name = '${name}', num = ${num}
    WHERE room_id = ${roomId}`);
    res.send('success');
  } catch (error) {
    res.send(error);
    throw (error);
  }
})

module.exports = router;
